<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/03/23 01:49:40 $)
// $RCSfile: rss_view.php,v $ : $Revision: 1.3 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

require_once ("./includes/functions_html.php");
require_once ("./includes/functions_xml.php");
$xml = new XML;
$grid = new Grid();
$template->languagetabletype = "file";
$template->loadLanguageSection("staff_tickets", TEMPLATE_FILE);
require_once ("./modules/tickets/functions_ticketsui.php");
require_once ("./modules/tickets/functions_rss.php");

// ======= CHECK SETTING =======
if ($_SWIFT["settings"]["t_enrss"] != 1)
{
	echo $_SWIFT["language"]["rssseterror"];
	exit;
}

// ======= AUTHENTICATE THE STAFF =======
if (!isset($_SERVER["PHP_AUTH_USER"]))
{
	authenticateViaHTTP();
} else if (isset($_SERVER["PHP_AUTH_USER"]) && isset($_SERVER["PHP_AUTH_PW"])) {
	$_staff = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."staff` WHERE `username` = '". $dbCore->escape($_SERVER["PHP_AUTH_USER"]) ."';");
	if (!empty($_staff["staffid"]) && $_staff["password"] == md5($_SERVER["PHP_AUTH_PW"]) && trim($_staff["password"]) != "")
	{
		unset($_staff["password"]);
		$_SWIFT["staff"] = $_staff;
	}
}

if (empty($_SWIFT["staff"]["staffid"]))
{
	authenticateViaHTTP();
}

// ======= IF WE REACHED TILL THIS POINT THEN WE ARE A GENUINE STAFF =======
$_assigns = getAssignedDepartments(MODULE_TICKETS);

$_ticketviewid = getDefaultTicketView($_SWIFT["staff"]["staffid"]);

$_departmentid = array();
if (!empty($_REQUEST["departmentid"]) && in_array($_REQUEST["departmentid"], $_assigns))
{
	$_departmentid[] = $_REQUEST["departmentid"];
} else {
	$_departmentid = $_assigns;
}

$_ticketstatusid = array();
if (!empty($_REQUEST["ticketstatusid"]))
{
	$_ticketstatusid[] = intval($_REQUEST["ticketstatusid"]);
} else {
	$_ticketstatusid = getDisplayTicketStatusList();
}

$_tickets = customViewProcess($_ticketviewid, $_departmentid, $_ticketstatusid, CUSTOMVIEW_FETCH);

/**
* ###############################################
* GENERATE THE XML
* ###############################################
*/
$xml->addParentTag("rss", array("version" => "2.0"));
	$xml->addParentTag("channel");
		$xml->addTag("title", sprintf($_SWIFT["language"]["ticketrssfeed"], $_SWIFT["settings"]["general_companyname"]));
		$xml->addTag("link", $_SWIFT["swiftpath"]."index.php?_m=tickets&_a=rss");
		$xml->addTag("description", sprintf($_SWIFT["language"]["ticketrssfeed"], $_SWIFT["settings"]["general_companyname"]));
		$xml->addTag("language", $_SWIFT["adminlanguage"]);
		$xml->addTag("copyright", $_SWIFT["settings"]["general_companyname"]);

		foreach ($_tickets as $key=>$val)
		{
			$xml->addParentTag("item");
				$xml->addTag("title", sprintf($_SWIFT["language"]["rsstitleformat"], iif($_SWIFT["settings"]["t_eticketid"]=="seq",$val["ticketid"], $val["ticketmaskid"]), htmlspecialchars($val["subject"])));
				$xml->addTag("link", $_SWIFT["swiftpath"]."staff/index.php?_m=tickets&_a=viewticket&ticketid=". intval($val["ticketid"]));
				$xml->addTag("description", htmlspecialchars($val["preview"]));
			$xml->endParentTag("item");
		}

	$xml->endParentTag("channel");
$xml->endParentTag("rss");


header("Content-Type: text/xml".SWIFT_CRLF);
echo $xml->returnXML();
?>